package com.zlkj.pro.modular.wechat.listener;

import com.zlkj.pro.core.expander.WxConfigExpander;
import com.zlkj.pro.modular.app.service.AppUsersService;
import com.zlkj.pro.modular.wechat.event.TemplateBean;
import com.zlkj.pro.modular.wechat.event.TemplateEvent;
import com.zlkj.pro.modular.wechat.model.param.SendTemplateMsgParam;
import com.zlkj.pro.modular.wechat.service.WeChatService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.zlkj.pro.modular.wechat.enums.WeChatEnum.*;

@Slf4j
@Component
public class TemplateListener implements ApplicationListener<TemplateEvent> {

    @Resource
    private WeChatService weChatService;
    @Resource
    private AppUsersService appUsersService;

    @Async
    @Override
    public void onApplicationEvent(TemplateEvent event) {

        log.info("监听公众号模板消息事件！");
        TemplateBean templateBean = event.getTemplateBean();

        switch (templateBean.getType()) {
            case WX_TEMPLATE_TYPE_0:
                log.info("公众号模板消息事件，用户订阅");
                log.info("关注公众号，openId【{}】", templateBean.getOpenId());
                //此处将公众号openId存到用户表中，用于后续发送消息
                appUsersService.bindWxMpOpenId(templateBean.getOpenId());
                break;
            case WX_TEMPLATE_TYPE_1:
                log.info("公众号模板消息事件，取消订阅");
                //此处可针对取消订阅的用户做业务处理(如果需要)

                break;
            case WX_TEMPLATE_TYPE_2:
                log.info("发送业务模板消息");
                String wxAppId = WxConfigExpander.getWxAppId();

                List<SendTemplateMsgParam> templateMessageList = templateBean.getOpenIds().stream().map(openId -> {
                    //构建模板参数
                    SendTemplateMsgParam templateMessage = SendTemplateMsgParam.builder()
                            .toUser(openId)
                            .templateId(templateBean.getTemplateId())
                            .appid(wxAppId)
                            .pagePath("pages/index/index")
                            .build();

                    //实际模板内容，按照模板内容key填充
                    templateMessage.addData(new WxMpTemplateData("character_string2", templateBean.getValue1(), "#000000"));
                    templateMessage.addData(new WxMpTemplateData("thing4", templateBean.getValue2(), "#000000"));
                    templateMessage.addData(new WxMpTemplateData("thing6", templateBean.getValue3(), "#000000"));
                    templateMessage.addData(new WxMpTemplateData("thing8", templateBean.getValue4(), "#000000"));
                    templateMessage.addData(new WxMpTemplateData("time9", templateBean.getValue5(), "#000000"));

                    return templateMessage;
                }).collect(Collectors.toList());

                //发送
                weChatService.send(templateMessageList);
                break;
            default:
        }

    }

}
